<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class setting_class extends AWS_MODEL
{
	/**
     * 构造函数
     */
	
	function get_vars_by_group($group_id = 0, $with_select = false)
	{
		$system_setting = (array)AWS_APP::config()->get('admin/system_setting');
		
		if($group_id > 0)
		{
			$group_setting = $system_setting[$group_id];
			
			if(empty($group_setting))
			{
				return false;
			}
			
			$var_names = array();
			
			foreach ($group_setting['children'] as $key => $val)
			{
				$var_names[] = $val['varname'];
			}
			
			$setting_array[$group_id] = $group_setting;
			
			$vars = $this->fetch_all('system_setting', 'varname IN (\'' . implode("','", $var_names) . '\')', 'id ASC');
		}
		else 
		{
			$setting_array = $system_setting;
			
			$vars = $this->fetch_all('system_setting', null, 'id ASC');
		}
		
		$setting = array();
		
		foreach($vars as $key => $val)
		{
			$setting[$val['varname']] = unserialize($val['value']);
		}
		
		foreach ($setting_array as $key => $group)
		{
			if($group['status'] != 0)
			{
				unset($setting_array[$key]);
				continue;
			}
			
			foreach($group['children'] as $gkey => $val)
			{
				if($val['status'] != 0)
				{
					unset($setting_array[$key]['children'][$gkey]);
					continue;
				}
				
				if(!array_key_exists($val['varname'], $setting))
				{
					unset($setting_array[$key]['children'][$gkey]);
					continue;
				}
				
				$setting_array[$key]['children'][$gkey]['value'] = $setting[$val['varname']];
				
				if ($with_select && ($val['type'] == 'select'))
				{
					if(($val['varname'] == 'ui_style'))
					{
						$setting_array[$key]['children'][$gkey]['select_list'] = $this->get_ui_styles();
					}
					else
					{
						$setting_array[$key]['children'][$gkey]['select_list'] = $this->get_select_config($val['varname']);
					}
				}
			}
		}
		
		return $setting_array;
	}
	
	function get_setting($where = null)
	{
		$rs = $this->fetch_all('system_setting', $where, 'id ASC');

		$settings = array();
		
		foreach ($rs as $key => $val)
		{
			$settings[$val['varname']] = unserialize($val['value']);
		}
		
		return $settings;
	}

	public function get_select_config($varname)
	{
		return (array) AWS_APP::config()->get('admin/select_' . $varname);
	}

	/**
	 * 根据变量名获得设置记录
	 * Enter description here ...
	 * @param unknown_type $key
	 */
	function get_vars_by_name($key)
	{
		if (empty($key))
		{
			return false;
		}
		
		$vars = $this->fetch_row('system_setting', "varname='" . $key . "'", 'id ASC');
		
		if (! is_array($vars))
		{
			return false;
		}
		
		return $vars['value'];
	}

	/**
	 * 检查过滤系统识别的参数
	 * Enter description here ...
	 * @param unknown_type $input
	 */
	function check_vars($input)
	{
		if (empty($input))
		{
			return false;
		}
		
		$data = $this->fetch_all('system_setting', "varname IN ('" . implode("','", array_keys($input)) . "')");
		
		if (empty($data))
		{
			return false;
		}
		
		$validted_vars = array();
		
		foreach ($data as $val)
		{
			$validted_vars[] = $val['varname'];
		}
		
		foreach ($input as $key => $val)
		{
			if (in_array($key, $validted_vars))
			{
				$r_vars[$key] = $val;
			}
		}
		
		return $r_vars;
	}

	/**
	 * 保存设置参数
	 * Enter description here ...
	 * @param unknown_type $vars
	 */
	function set_vars($vars)
	{
		if (empty($vars))
		{
			return false;
		}
		
		foreach ($vars as $key => $val)
		{
			$update_arr['value'] = serialize($val);
			
			$this->update('system_setting', $update_arr, "varname='" . $key . "'");
		}
		
		return true;
	}

	/**
	 * 将系统设置保存到缓存文件
	 * Enter description here ...
	 */
	function update_setting_config()
	{
		$settings = $this->get_setting();
		
		if (empty($settings) && ! is_array($settings))
		{
			return false;
		}
		
		return AWS_APP::config()->set('setting', $settings);
	}

	public function get_ui_styles()
	{
		$exclude_dir = array('.', '..', '.svn');
		
		$dirs = array();
		
		if ($handle = opendir('./views'))
		{
			while (false !== ($file = readdir($handle)))
			{
				if(!in_array($file, $exclude_dir) && is_dir('./views/' . $file))
				{
					$dirs[] = $file;
				}
			}
			
			closedir($handle);
		}
		
		$ui_style = array();
			
		foreach($dirs as $key => $val)
		{
			$ui_style[] = array(
					'id' => $val,
					'title' => $val,
			);
		}
		
		return $ui_style;
	}
	
}
